// pages/insurance/orderPay/index.js
import urls from '../../../requestUrl'
import request from '../../../baseRequest'

let t1;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderInfo: {},
        agreementContent: '',
        agreementChecked: true,
        actionBtns: [{
                text: '二维码支付',
                bgColor: '#2d8cf0',
                id: 1,
            },
            {
                text: '微信支付',
                bgColor: '#19be6b',
                id: 2
            }
        ],
        showQRcode: false
    },
    handleToPay(e) {
        this.setData({
            showModal: true
        })
    },
    handleClickPayType(e) {
        this.setData({
            showModal: false
        })
    },
    changeAgreementCheckBox(e) {
        console.log(e)
        this.setData({
            agreementChecked: !this.data.agreementChecked
        })
    },
    handleActionBtnClick(e) {
        if (!this.data.agreementChecked) {
            wx.showToast({
                title: '请阅读并确认服务协议',
                icon: 'none'
            })
            return
        }

        let btnId = e.detail.id
        console.log('handleActionBtnClick', btnId)
        if (btnId === '1') {
            this.getOrderQRcode()
            this.setData({
                showQRcode: true
            })
        } else {
            this.requestPaymentParm()
        }

    },

    /**关闭弹出层 */
    closeRightPop() {
        if (t1) {
            clearTimeout(t1)
        }
        this.setData({
            showQRcode: false
        })
    },

    /**获取订单支付详情 */
    getOrderDetailInfo() {
        let url = `${urls.insurance.paymentDetailOrder}/${this.data.orderId}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                orderInfo: res.orderInfo,
                agreementContent: res.agreementContent
            })
        }, () => {})
    },

    /**请求微信支付 */
    requestPaymentParm() {
        let url = `${urls.insurance.paymentOrder}/${this.data.orderId}`
        request.postRequest(url, {}, (res) => {
            console.log(res)
                // this.handelSuccessPayment()
            wx.requestPayment({
                timeStamp: res.timestamp,
                nonceStr: res.nonce_str,
                package: res.package,
                signType: 'MD5',
                paySign: res.pay_sign,
                success: (res) => {
                    this.handelSuccessPayment()
                },
                fail: function() {
                    // fail
                    wx.showModal({
                        title: '温馨提醒',
                        content: '支付未成功！请重新发起支付',
                        showCancel: false,
                        confirmText: '确认关闭'
                    })
                },
                complete: function() {
                    // complete
                }
            })

        }, () => {})
    },
    /**支付成功统一处理 */
    handelSuccessPayment() {
        if (t1) {
            clearTimeout(t1)
        }
        wx.showToast({
            title: '支付成功'
        })
        wx.redirectTo({
            url: `/pages/insurance/orderComplete/index?oid=${this.data.orderId}`
        })
        return
        if (this.data.routerType == '1') {
            wx.navigateBack({})
        } else {

        }
    },

    /**获取二维码 */
    getOrderQRcode() {
        let param = {
            orderId: this.data.orderId
        }
        request.postRequest(urls.insurance.getPayQRcode, param, (res) => {
            if (res) {
                this.setData({
                    qrImgUrl: `${urls.baseUrl}${urls.insurance.showPayQRcode}?url=${res}`
                })
            }
        }, () => {
            this.checkPaymentStatus()
        })
    },

    /**支付回调 */
    checkPaymentStatus() {
        let url = `${urls.insurance.checkPayStatus}/${this.data.orderId}`
        request.getRequest(url, {}, (res) => {
            console.log('支付状态', res)
            switch (res) {
                case '0':
                    t1 = setTimeout(() => {
                        this.checkPaymentStatus()
                    }, 3000);
                    break
                case '4':
                    this.handelSuccessPayment()
                    break
            }
        }, () => {})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            orderId: options.id
        })
        this.data.routerType = options.type || '1'
        this.getOrderDetailInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        if (t1) {
            clearTimeout(t1)
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        if (t1) {
            clearTimeout(t1)
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})